package com.smarthome.core.message.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShSmsTempletService;
import com.smarthome.core.message.MessageTemplateService;
import com.smarthome.domain.bean.ShSmsTemplet;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

@Service("messageTemplateService")
public class MessageTemplateServiceImpl implements MessageTemplateService {

	@Resource
	private ShSmsTempletService templetService;

	@Override
	public DataGrid dataGrid(PageHelper page,ShSmsTemplet templet) {
		return templetService.queryShSmsTemplates(page,templet);
	}

	@Override
	public ShSmsTemplet queryMessageTemplateById(Long templateId) {
		return templetService.selectEntryById(templateId);
	}

	@Override
	public void saveOrEdit(ShSmsTemplet template) {
		templetService.saveOrUpdate(template);
	}

	@Override
	public ShSmsTemplet queryTempletById(Long id) {
		return templetService.selectEntryById(id);
	}

	@Override
	public int delete(Long id) {
		return templetService.deleteByIds(id);
	}

	@Override
	public Result saveOrUpdate(ShSmsTemplet template) {
		ShSmsTemplet condtion = new ShSmsTemplet();
		condtion.setIsUsed(1);
		condtion.setTypeId(template.getTypeId());
		condtion.setResidenceId(template.getResidenceId());
		List<ShSmsTemplet> lets = templetService.selectEntryList(condtion);
		if (lets == null || lets.size() <= 1 || template.getIsUsed() == 0) {
			if(lets.size() == 1 && ! lets.get(0).getId().equals(template.getId()) )
				return Result.failure("操作失败，该类型已有启用的模版！");
			templetService.saveOrUpdate(template);
			return Result.success("操作成功！");
		} else {
			return Result.failure("操作失败，该类型已有启用的模版！");
		}
	}

}
